<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>组件通信-子组件向父组件传递数据</title>
</head>
<script src="../js/vue.js"></script>
<body>

<div id="app">
    <cpn @item-click="cpnClick"></cpn>
</div>

</body>

<template id="cpn">
    <div>
        <button v-for="item in categories" @click="btnClick(item)">{{item.name}}</button>
    </div>
</template>

<script>
    // 子组件
    const cpn = {
        template: '#cpn',
        data() {
            return {
                categories: [
                    {id: 'aaa', name: '热门推荐'},
                    {id: 'bbb', name: '手机数码'},
                    {id: 'ccc', name: '家用家电'},
                    {id: 'ddd', name: '电脑办公'},
                ]
            }
        },
        methods: {
            btnClick(item) {
                // alert(item.name);
                this.$emit('item-click', item);
            }
        }
    };

    const vue = new Vue({
        'el': '#app',
        'data': {},
        components: {
            cpn
        },
        methods:{
            cpnClick(item) {
                alert(item.name);
            }
        }
    })
</script>
</html>